#define  _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAXN 1000

typedef struct {
    int x, y, z;
} Village;

typedef struct {
    int u, v;
    double cost;
    double length;
} Edge;

Village villages[MAXN];
Edge edges[MAXN * MAXN];
int parent[MAXN];
int rank[MAXN];

int find(int u) {
    if (parent[u] != u) {
        parent[u] = find(parent[u]);
    }
    return parent[u];
}

void union_set(int u, int v) {
    int root_u = find(u);
    int root_v = find(v);
    if (root_u != root_v) {
        if (rank[root_u] > rank[root_v]) {
            parent[root_v] = root_u;
        }
        else if (rank[root_u] < rank[root_v]) {
            parent[root_u] = root_v;
        }
        else {
            parent[root_v] = root_u;
            rank[root_u]++;
        }
    }
}

int compare_edges(const void* a, const void* b) {
    Edge* edge_a = (Edge*)a;
    Edge* edge_b = (Edge*)b;
    if (edge_a->cost < edge_b->cost) return -1;
    if (edge_a->cost > edge_b->cost) return 1;
    return 0;
}

int main() {
    int N;
    while (scanf("%d", &N) == 1 && N != 0) {
        for (int i = 0; i < N; i++) {
            scanf("%d %d %d", &villages[i].x, &villages[i].y, &villages[i].z);
            parent[i] = i;
            rank[i] = 0;
        }

        int edge_count = 0;
        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                double dx = villages[i].x - villages[j].x;
                double dy = villages[i].y - villages[j].y;
                double dz = villages[i].z - villages[j].z;
                double length = sqrt(dx * dx + dy * dy);
                double cost = fabs(dz);
                edges[edge_count].u = i;
                edges[edge_count].v = j;
                edges[edge_count].cost = cost;
                edges[edge_count].length = length;
                edge_count++;
            }
        }

        qsort(edges, edge_count, sizeof(Edge), compare_edges);

        double total_cost = 0;
        double total_length = 0;
        int edge_added = 0;

        for (int i = 0; i < edge_count && edge_added < N - 1; i++) {
            int u = edges[i].u;
            int v = edges[i].v;
            if (find(u) != find(v)) {
                union_set(u, v);
                total_cost += edges[i].cost;
                total_length += edges[i].length;
                edge_added++;
            }
        }

        printf("%.3f\n", total_cost / total_length);
    }

    return 0;
}